package com.ls.service.impl;

import com.ls.dao.EmployeeDao;
import com.ls.entity.Employee;
import com.ls.entity.JsonResult;
import com.ls.entity.Page;
import com.ls.service.IEmployeeService;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;



@Service("employeeService")
public class EmployeeServiceImpl implements IEmployeeService {
	@Autowired
	private EmployeeDao dao;

	public JsonResult<Employee> checkLogin(String email, String password) {

		JsonResult<Employee> result = new JsonResult<Employee>();
		// 判断邮箱正则
		String email_regex = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
		if (!email.matches(email_regex)) {
			result.setStatus(3);
			result.setMsg("邮箱格式错误");
			return result;
		}

		Employee employee = dao.findByEmail(email);
		if (employee == null) {
			// 账号不存在
			result.setStatus(1);
			result.setMsg("邮箱不存在");
			return result;
		}
		String pwd = EmpUtil.md5(password);
		if (!employee.getPassword().equals(pwd)) {
			result.setStatus(2);
			result.setMsg("密码错误");
			return result;
		}
		result.setStatus(0);
		result.setData(employee);
		return result;

	}
	/**
	 * 查询员工
	 */
	public JsonResult<List<Employee>> findAllEmployee() {
		JsonResult<List<Employee>> result = new JsonResult<List<Employee>>();
		List<Employee> employees = dao.findAllEmployee();
		if (employees == null) {
			result.setStatus(1);
			result.setMsg("查询员工失败");
			return result;
		}
		result.setStatus(0);
		result.setData(employees);
		return result;
	}
	/**
	 * 查询员工
	 */
	public JsonResult<List<Employee>> findEmployeeFenye(int pageId, int num) {
		JsonResult<List<Employee>> result = new JsonResult<List<Employee>>();
		Page page = new Page();
		int jilu = dao.findAllEmployee().size();
		List<Employee> list = dao.findEmployeeFenye((pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setPage(page);
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询产品分页出错");
		return result;
	}
	/**
	 * 插入员工
	 */
	public JsonResult<Object> insertEmployee(String emp_name, int age, String email, String password, int dept_id,
											 String hire_date, String info) {
		JsonResult<Object> result = new JsonResult<Object>();
		// 判断邮箱正则
		String email_regex = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
		if (!email.matches(email_regex)) {
			result.setStatus(2);
			result.setMsg("邮箱格式错误");
			return result;
		}
		// 密码加密
		String md5 = EmpUtil.md5(password);
		// 日期格式转换
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		try {
			date =format.parse(hire_date);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Employee employee = new Employee();
		employee.setEmp_name(emp_name);
		employee.setAge(age);
		employee.setEmail(email);
		employee.setPassword(md5);
		employee.setDept_id(dept_id);
		employee.setHire_date(new Date(date.getTime()));
		employee.setInfo(info);
		int i = dao.insertEmployee(employee);
		if (i != 1) {
			result.setStatus(1);
			result.setMsg("添加员工失败");
			return result;
		}
		result.setStatus(0);
		result.setData(employee);
		return result;
	}
	/**
	 * 删除员工
	 */
	public JsonResult<Object> deleteByIdEmp(String[] emp_id) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.deleteByIdEmp(emp_id);
		//失败
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("删除员工失败");
			return result;
		}
		result.setStatus(0);
		result.setData(i);
		return result;
	}
	/**
	 *     搜索员工
	 */
	public JsonResult<List<Employee>> findName(String name, int pageId, int num) {
		JsonResult<List<Employee>> result = new JsonResult<List<Employee>>();
		Page page = new Page();
		int jilu = dao.findName(name).size();
		List<Employee> list = dao.findNameFenye(name, (pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			result.setPage(page);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询失败");
		return result;
	}

	
	public JsonResult<Employee> showEmp(int emp_id) {
		JsonResult<Employee> result = new JsonResult<Employee>();
		Employee emp = dao.showEmp(emp_id);
		if (emp!=null) {
			result.setStatus(0);
			result.setData(emp);
			return result;
		}
		result.setStatus(1);
		result.setMsg("show员工失败了");
		return result;
	}

	public JsonResult<Object> updateEmployee(Employee employee) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.update(employee);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改员工失败了");
		return result;
	}
	public JsonResult<Object> upinfo(int emp_id, String emp_name, String email, String info) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.upinfo(emp_id,emp_name,email,info);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改info失败了");
		return result;
	}
	public JsonResult<Object> uppwd(int emp_id,String password) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.uppwd(emp_id,password);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改pwd失败了");
		return result;
	}
	public JsonResult<Employee> selectpwd(int emp_id, String password) {
		JsonResult<Employee> result = new JsonResult<Employee>();
		Employee employee = dao.selectpwd(emp_id);
		if (employee==null) {
			result.setStatus(1);
			result.setMsg("出错了");
			return result;
		}
		if(!employee.getPassword().equals(EmpUtil.md5(password))) {
			result.setStatus(2);
			result.setMsg("密码不正确");
			return result;
		}
		result.setStatus(0);
		result.setData(employee);
		return result;
	}

}
